package com.foot.travel.wxapi;

import android.app.Activity;
import com.foot.travel.R;
import android.os.Bundle;
import android.widget.Toast;

import com.koalac.ironman.weixin.FootTravelWechat;
import com.koalac.ironman.weixin.WXLoginHelper;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    WXLoginHelper mWXLoginHelper;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_wxentry);
	FootTravelWechat.INSTANCE.getWXAPI(WXEntryActivity.this)
	.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq rep) {
	
    }

    @Override
    public void onResp(BaseResp resp) {
	int result = 0;
	switch (resp.errCode) {
	case BaseResp.ErrCode.ERR_OK:
		int type = resp.getType();
		switch (type) {
		/**
		 * 微信登陆成功
		 */
		case ConstantsAPI.COMMAND_SENDAUTH:
			SendAuth.Resp sendResp = (SendAuth.Resp) resp;
			/**
			 * 用户换取access_token的code，仅在ErrCode为0时有效
			 */
			String code = sendResp.code;
			if (mWXLoginHelper == null) {
				mWXLoginHelper = new WXLoginHelper(WXEntryActivity.this);
			}
			mWXLoginHelper.getAccessToken(code);
			return;
		}

		break;
	case BaseResp.ErrCode.ERR_USER_CANCEL:
		break;
	case BaseResp.ErrCode.ERR_AUTH_DENIED:
		break;
	default:
		break;
	}
	Toast.makeText(this, result, Toast.LENGTH_LONG).show();
	WXEntryActivity.this.finish();
    }

}
